from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
# Create your views here.


# 查询所有的员工、新增一个员工
class StaffAPIView(APIView):
    # 接收前端的GET请求
    def get(self, request): # request 请求对象
        # 查询所有的员工，返回的查询集合
        staff_queryset = Staff.objects.all()

        # 手动序列化--自己组织json格式
        data_list = []
        for obj in staff_queryset:
            temp = {
                "id": obj.id,
                "name": obj.name,
                "sex": obj.get_sex_display(),
                "phone": obj.phone,
                "email": obj.email,
                "dep": obj.dep.name,
                "duty": obj.duty.name
            }

            data_list.append(temp)
        # 返回响应
        return Response({
            "code": 200,
            "msg": "加载所有员工数据",
            "data": data_list
        })

    # 添加一个员工
    def post(self, request):
        # 接收前端数据
        name = request.data.get("name")
        sex = request.data.get("sex")  # 1 or 2
        phone = request.data.get("phone")
        email = request.data.get("email")
        dep = request.data.get("dep") # 1
        duty = request.data.get("duty")  # 1

        # 正常开发时，要对数据进行基本的 校验

        # 存入数据库
        Staff.objects.create(name=name, sex=sex, phone=phone, email=email,
                             dep_id=dep, duty_id=duty)

        return Response({
            "code": 200,
            "msg": "添加成功"
        })


# 查询一个、更新一个、删除一个
class StaffDetailAPIView(APIView):
    def get(self, request, pk):
        # 根据pk 查询一个员工
        obj = Staff.objects.filter(id=pk).first()

        if obj:
            # 对象存在，则返回
            return Response({
                "code": 200,
                "msg": "查询一个员工成功",
                "data": {
                    "id": obj.id,
                    "name": obj.name
                }
            })
        else:
            return Response({
                "code": 400,
                "msg": "当前员工不存在"
            })

    def put(self, request, pk):
        # 接收前端数据
        name = request.data.get("name")
        sex = request.data.get("sex")
        phone = request.data.get("phone")
        email = request.data.get("email")
        dep = request.data.get("dep")
        duty = request.data.get("duty")

        # 根据pk 查询一个员工
        obj = Staff.objects.filter(id=pk).first()
        obj.name = name
        obj.sex = sex
        obj.phone = phone
        obj.email = email
        obj.dep_id = dep
        obj.duty_id = duty

        obj.save()

        return Response({
            "code": 200,
            "msg": "更新成功"
        })

    def delete(self, request, pk):
        # 根据pk 查询对象
        obj = Staff.objects.filter(id=pk).first()
        if obj:
            obj.delete()
            return Response({
                "code": 200,
                "msg": "删除成功"
            })
        else:
            return Response({
                "code": 400,
                "msg": "当前员工不存在"
            })